package com.showde.shoppingmall.adapter;

import java.util.List;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.domain.Product;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RecommendAdapter extends BaseAdapter {

	private List<Product> products;
	private Context context;

	ImageLoader imageLoader;

	public RecommendAdapter(Context context, List<Product> products,
			ImageLoader imageLoader) {
		this.context = context;
		this.products = products;
		this.imageLoader = imageLoader;
	}

	public void resetData(List<Product> products) {
		this.products = products;
	}

	@Override
	public int getCount() {
		return products.size();
	}

	@Override
	public Product getItem(int position) {
		return products.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_in_recommend, null);
			holder = new ViewHolder();

			holder.pLogo = (ImageView) convertView.findViewById(R.id.img_plogo);
			holder.pDesc = (TextView) convertView.findViewById(R.id.txt_pdesc);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Product product = getItem(position);
		imageLoader.displayImage(product.getProductLogo(), holder.pLogo);
		holder.pDesc.setText(product.getProductTitle());

		return convertView;
	}

	private final class ViewHolder {
		ImageView pLogo;
		TextView pDesc;
	}

}
